<?php

define('ROOT', dirname(__FILE__) . '/..');

require ROOT . '/system/autoloader.php';

try {
    main();
} catch (Exception $e) {
    if (strpos($_SERVER['SERVER_NAME'], '.local') !== FALSE) {
        // локальеый вариант.
        echo '<h1>', $e->getMessage(), '</h1>';
        echo '<pre>', $e->getTraceAsString(), '</pre>';
    } else {
        // production
        header('Location:/500.html');
    }
}

function main() {
    $router = new Router;

    list($class, $method, $param_arr) = $router->get();

    $content = call_user_func_array(array(new $class, $method), $param_arr);

    $view = new View;
    echo $view->render($class, $method, $content);
}

function get_config($name) {
    $config = ROOT . "/system/config/{$name}.php";
    if (is_file($config)) {
        return require_once $config;
    }
    throw new Exception('Config not found');
}